Imports cmplib01
Imports System.IO


Public Class ctlMessageCenter
    Inherits System.Windows.Forms.UserControl

    Private ptrCurrentGame As CampaignerGameData

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'UserControl overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents pnlTextPanel As System.Windows.Forms.Panel
    Friend WithEvents lblText As System.Windows.Forms.Label
    Friend WithEvents btnCompose As System.Windows.Forms.Button
    Friend WithEvents lstMessages As System.Windows.Forms.ListBox
    Friend WithEvents txtMessageText As System.Windows.Forms.TextBox
    Friend WithEvents btnDeleteSelected As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.pnlTextPanel = New System.Windows.Forms.Panel()
        Me.lblText = New System.Windows.Forms.Label()
        Me.btnCompose = New System.Windows.Forms.Button()
        Me.lstMessages = New System.Windows.Forms.ListBox()
        Me.txtMessageText = New System.Windows.Forms.TextBox()
        Me.btnDeleteSelected = New System.Windows.Forms.Button()
        Me.pnlTextPanel.SuspendLayout()
        Me.SuspendLayout()
        '
        'pnlTextPanel
        '
        Me.pnlTextPanel.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right)
        Me.pnlTextPanel.BackColor = System.Drawing.Color.SteelBlue
        Me.pnlTextPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.pnlTextPanel.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblText})
        Me.pnlTextPanel.Name = "pnlTextPanel"
        Me.pnlTextPanel.Size = New System.Drawing.Size(488, 32)
        Me.pnlTextPanel.TabIndex = 5
        '
        'lblText
        '
        Me.lblText.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblText.Location = New System.Drawing.Point(8, 4)
        Me.lblText.Name = "lblText"
        Me.lblText.Size = New System.Drawing.Size(152, 24)
        Me.lblText.TabIndex = 0
        Me.lblText.Text = "Message Center"
        '
        'btnCompose
        '
        Me.btnCompose.BackColor = System.Drawing.Color.PaleGoldenrod
        Me.btnCompose.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnCompose.ForeColor = System.Drawing.Color.Black
        Me.btnCompose.Location = New System.Drawing.Point(8, 40)
        Me.btnCompose.Name = "btnCompose"
        Me.btnCompose.TabIndex = 6
        Me.btnCompose.Text = "Compose"
        '
        'lstMessages
        '
        Me.lstMessages.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left)
        Me.lstMessages.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.lstMessages.Location = New System.Drawing.Point(96, 40)
        Me.lstMessages.Name = "lstMessages"
        Me.lstMessages.Size = New System.Drawing.Size(168, 80)
        Me.lstMessages.TabIndex = 7
        '
        'txtMessageText
        '
        Me.txtMessageText.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right)
        Me.txtMessageText.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.txtMessageText.Location = New System.Drawing.Point(272, 40)
        Me.txtMessageText.Multiline = True
        Me.txtMessageText.Name = "txtMessageText"
        Me.txtMessageText.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtMessageText.Size = New System.Drawing.Size(208, 80)
        Me.txtMessageText.TabIndex = 8
        Me.txtMessageText.Text = ""
        '
        'btnDeleteSelected
        '
        Me.btnDeleteSelected.BackColor = System.Drawing.Color.PaleGoldenrod
        Me.btnDeleteSelected.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnDeleteSelected.ForeColor = System.Drawing.Color.Black
        Me.btnDeleteSelected.Location = New System.Drawing.Point(8, 72)
        Me.btnDeleteSelected.Name = "btnDeleteSelected"
        Me.btnDeleteSelected.TabIndex = 9
        Me.btnDeleteSelected.Text = "Delete"
        '
        'ctlMessageCenter
        '
        Me.BackColor = System.Drawing.Color.LightSkyBlue
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnDeleteSelected, Me.txtMessageText, Me.lstMessages, Me.btnCompose, Me.pnlTextPanel})
        Me.Name = "ctlMessageCenter"
        Me.Size = New System.Drawing.Size(488, 128)
        Me.pnlTextPanel.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub lstMessages_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstMessages.Resize
        txtMessageText.Height = lstMessages.Height
    End Sub


    Public Sub ClearData()
        btnCompose.Enabled = False
        btnDeleteSelected.Enabled = False
        lstMessages.Items.Clear()
        txtMessageText.Text = ""
    End Sub

    Public Sub ShowMessages(ByRef currentGame As CampaignerGameData)
        ptrCurrentGame = currentGame

        ClearData()

        btnCompose.Enabled = True
        btnDeleteSelected.Enabled = True

        Dim messKey As String
        Dim mess As Messagem

        For Each messKey In ptrCurrentGame.Messages_g.HT.Keys
            mess = ptrCurrentGame.Messages_g.Item(messKey)
            If mess.ReceivingPlayerID = ptrCurrentGame.selectedPlayer.PlayerID Then
                If mess.Viewed = False Then
                    lstMessages.Items.Add(mess)
                End If
            End If
        Next

    End Sub

    Private Sub DisplayMessagem(ByVal mess As Messagem)
        Dim sendPlay As Player
        sendPlay = ptrCurrentGame.Players.Item(CampaignerGameData.CDGKEY_PLAYER & mess.SendingPlayerID)

        Dim header As String = "Sent by: " & sendPlay.PlayerName & " on turn " & mess.TurnActive & ControlChars.CrLf

        txtMessageText.Text = header & mess.Message

    End Sub

    Private Sub btnCompose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompose.Click
        Dim frmCompose As New frmComposeMessage(ptrCurrentGame)

        frmCompose.ShowDialog()

    End Sub

    Private Sub lstMessages_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstMessages.SelectedIndexChanged
        ShowSelectedMessage()
    End Sub

    Private Sub ShowSelectedMessage()
        ' HACK: this crappy sub is due DIRECTLY to my lazy handling of the CORE of this program. Good idea meathead.
        If lstMessages.SelectedItem Is Nothing Then Exit Sub

        Try
            Dim mess As Messagem = lstMessages.SelectedItem
            Dim messText As String
            Dim sendplayName As String

            ' TODO: use stringbuilder when you find what damn library its in
            Try
                Dim sendPlayKey As String
                Dim sendPlay As Player
                sendPlayKey = ptrCurrentGame.ReturnPlayerKeyWithID(mess.SendingPlayerID)
                sendPlay = ptrCurrentGame.Players.Item(sendPlayKey)
                sendplayName = sendPlay.PlayerName
            Catch argEx As ArgumentOutOfRangeException
                ' player is not in the game anymore 
                sendplayName = "inactive player"
            Catch ex As Exception
                HandleException(c_Module, ex)

            Finally
                messText = "Message Received from " & sendplayName & ControlChars.CrLf
                messText = messText & mess.Title.ToUpper & ControlChars.CrLf
                messText = messText & mess.Message

                txtMessageText.Text = messText
            End Try

        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try

    End Sub

    Private Sub DeleteSelectedMessage()
        If lstMessages.SelectedItem Is Nothing Then Exit Sub

        Try
            ptrCurrentGame.Messages_g.HT.Remove("MESS" & lstMessages.SelectedItem.messageid)
            ShowMessages(ptrCurrentGame)
        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try

    End Sub

    Private Sub btnDeleteSelected_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteSelected.Click
        DeleteSelectedMessage()
    End Sub
End Class
